from email.mime.text import MIMEText
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

def send_email(email, password, msg, smtp_server, port, to_email='18768121976@163.com'):
    from_addr = email
    password = password
    to_addr = to_email
    smtp_server = smtp_server

    msg = MIMEText(msg, 'plain', 'utf-8')
    msg['From'] = _format_addr('采购系统 <%s>' % from_addr)
    msg['To'] = _format_addr('部门经理 <%s>' % to_addr)
    msg['Subject'] = Header('计划单通知邮件', 'utf-8').encode()

    server = smtplib.SMTP(smtp_server, port)
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()

